package com.tangye.massageshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tangye.massageshop.entity.UserEntity;
import com.tangye.massageshop.entity.WorkerEntity;
import com.tangye.massageshop.enums.WorkStatusEnum;
import com.tangye.massageshop.enums.WorkerStatusEnum;
import com.tangye.massageshop.exception.ShopException;
import com.tangye.massageshop.mapper.WorkerMapper;
import com.tangye.massageshop.repository.WorkerRepository;
import com.tangye.massageshop.service.WorkerService;
import com.tangye.massageshop.util.SystemUtil;
import com.tangye.massageshop.util.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class WorkerServiceImpl implements WorkerService {

    @Resource
    private WorkerRepository workerRepository;

    @Resource
    private WorkerMapper workerMapper;
    @Override
    public List<WorkerEntity> getAll() {
        return workerMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public List<WorkerEntity> queryWorkerListPage(WorkerEntity workerEntity) {
        return workerRepository.selectWorkersPage(workerEntity);
    }

    @Override
    public void add(WorkerEntity workerEntity) {
        if (!SystemUtil.validateIdNumber(workerEntity.getWorkerCert())) {
            throw new ShopException("身份证号码不合法");
        }
        UserEntity userEntity = ThreadLocalUtil.get();
        workerEntity.setCompanyId(userEntity.getCompanyId());
        if (Objects.nonNull(userEntity.getShopId())) {
            workerEntity.setShopId(userEntity.getShopId());
        }
        workerEntity.setWorkerStatus(WorkerStatusEnum.TRY.getCode());
        workerEntity.setWorkStatus(WorkStatusEnum.LEISURE.getCode());
        workerMapper.insert(workerEntity);
    }

    @Override
    public void update(WorkerEntity workerEntity) {
        workerMapper.updateById(workerEntity);
    }
}
